package com.hsc.account;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.hsc.db.tools.AccountList;
import com.hsc.db.tools.AccountService;
import com.hsc.db.tools.EmailService;
import com.hsc.db.tools.ItemList;
import com.hsc.db.tools.ItemService;
import com.hsc.db.tools.UseItemService;
import com.hsc.db.tools.useitem;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;


public class search extends Dialog {

	public search(Context context) {
		super(context);
	}

	public search(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private GridView typeview;
		private int iyear;
		private int imonth;
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private  EditText  searchval;
		private AccountService accs;
		private ItemService items;
		private ListView listView;
		private int totalItem=0;



		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;

			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		public search create(Context context,AccountService accs1,int year,int month,final ItemService itemss) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final search dialog = new search(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.search, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			search.Builder.this.imonth = month;
			search.Builder.this.iyear = year;

			searchval = (EditText)layout.findViewById(R.id.searchRemark);

			searchval.requestFocus();
			accs = accs1;

			items = itemss;

			listView= (ListView)layout.findViewById(R.id.listView);

			listView.setOnScrollListener(new OnScrollListener() {
				private boolean isBottom = false;   //用于标记是否到达顶端
				//listview的状态发送改变时执行
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					if(isBottom && scrollState==OnScrollListener.SCROLL_STATE_IDLE){
						String re = searchval.getText().toString();

						List<AccountList> cur=null;
						if(re.toString().trim().length()>0)
						{
							if(re.startsWith("？") && re.indexOf("-")>0 && re.endsWith("？"))
							{
								String[] amt = re.substring(1, re.length()-1).split("-");

								if(amt.length==1)
								{
									cur=accs.getListByAmt(iyear, amt[0],"-",twoActivity.pagenum,totalItem);
								}else if(amt.length==2 && amt[0].length()==0)
								{
									cur=accs.getListByAmt(iyear,"-", amt[1],twoActivity.pagenum,totalItem);
								}else if(amt.length==2 && amt[0].length()>0)
								{
									cur=accs.getListByAmt(iyear,amt[0], amt[1],twoActivity.pagenum,totalItem);
								}

							}else
							{
								cur=accs.getListByTypeLike(iyear, re,twoActivity.pagenum,totalItem);
							}

						}else
						{
							cur=accs.getListByTypeLike(iyear, "@@",twoActivity.pagenum,totalItem);
						}

						ListViewAdapter adapter = (ListViewAdapter)listView.getAdapter();
						for(AccountList acc:cur)
						{
							adapter.getListItems().add(acc);
						}

						adapter.notifyDataSetChanged();
						isBottom = false;
					}
				}
				//在滚动的过程中不断执行
				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
									 int visibleItemCount, int totalItemCount) {

//			        	System.out.println("f="+firstVisibleItem+" v="+visibleItemCount+" t="+totalItemCount);
					if(firstVisibleItem+visibleItemCount == totalItemCount){
						totalItem=totalItemCount;
						isBottom = true;
					}else{
						isBottom = false;
					}
				}
			});


			searchval.addTextChangedListener(new TextWatcher() {

				// 第二个执行
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {

				}

				// 第一个执行
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
											  int after) {

				}

				// 第三个执行
				@Override
				public void afterTextChanged(Editable s) {
					String re = searchval.getText().toString();
					totalItem=0;
					List<AccountList> cur=null;
					if(re.toString().trim().length()>0)
					{
						if(re.startsWith("？") && re.indexOf("-")>0 && re.endsWith("？"))
						{
							String[] amt = re.substring(1, re.length()-1).split("-");
							if(amt.length==1)
							{
								cur=accs.getListByAmt(iyear, amt[0],"-",twoActivity.pagenum,totalItem);
							}else if(amt.length==2 && amt[0].length()==0)
							{
								cur=accs.getListByAmt(iyear,"-", amt[1],twoActivity.pagenum,totalItem);
							}else if(amt.length==2 && amt[0].length()>0)
							{
								cur=accs.getListByAmt(iyear,amt[0], amt[1],twoActivity.pagenum,totalItem);
							}else
							{
								cur=accs.getListByTypeLike(iyear, "@@",twoActivity.pagenum,totalItem);
							}

						}else
						{
							cur=accs.getListByTypeLike(iyear, re,twoActivity.pagenum,totalItem);
						}

					}else
					{
						cur=accs.getListByTypeLike(iyear, "@@",twoActivity.pagenum,totalItem);
					}

					//显示查询数据
					ListViewAdapter adapter = new ListViewAdapter(search.Builder.this.context,cur,search.Builder.this.items);
					listView.setAdapter(adapter);
					listView.setDivider(new ColorDrawable(Color.GRAY));
					listView.setDividerHeight(1);
				}
			});

			openInputMethod(searchval);

			dialog.setContentView(layout);


			return dialog;
		}

		public void openInputMethod(final EditText editText) {

			Timer timer = new Timer();

			timer.schedule(new TimerTask() {

				public void run() {

					InputMethodManager inputManager = (InputMethodManager) editText

							.getContext().getSystemService(

									Context.INPUT_METHOD_SERVICE);

					inputManager.showSoftInput(editText, 0);

				}

			}, 200);
		}

	}
}